/***    
* date：2018.03.08
* name: 购物车的中间件
* author：hj
* remarks：null
*/
var express   = require("express");
var app       =express();

// 解决跨域问题，网上成熟的解决方案，我是直接拷贝的
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

// index页面的header导航条 
app.get('/',function(req,res){

	res.send('购物车用的中间件');
	res.end();
});

// 产品详情页商品数量
app.get('/cart/goodsList',function(req,res){
    var  json = {
                    "goodsList": [
                        {
                            "goodsname": "商品名111",
                            "url": "http://img.hb.aicdn.com/c33a6d02f8f773902c305474aadf7cf1d1dff79b1a401-UcOTTj_fw658",
                            "nums": 2,
                            "price": 50,
                            "ischeck": 1,
                            "subtotal": 100
                        },
                        {
                            "goodsname": "商品名222",
                            "url": "http://img.hb.aicdn.com/3f900a6720f775b508b7f9b752d8f4652248b4a92f1b3-a3ks4J_fw658",
                            "nums": 10,
                            "price": 200,
                            "ischeck": 1,
                            "subtotal": 2000
                        },
                        {
                            "goodsname": "商品名333",
                            "url": "http://img.hb.aicdn.com/5228fd72efbc154f44199f33210b5fc208bc73a94a670-0IE307_fw658",
                            "nums": 5,
                            "price": 233,
                            "ischeck": 1,
                            "subtotal": 1165
                        }
                    ],
                            "totalprice": 3265,
                            "totalnums": 17
                }
    res.send(json);
    res.end();
});
// 增加商品数量和小计
app.get('/cart/cart_add',function(req,res){
    var _nums = req.query.num;
    var _price= req.query.price;
    console.log(req.query);
    _nums++;
    var json={
        'total' : _nums*_price,
        'num':_nums
    }
    res.send(json);
    res.end();
});
// 减少商品数量和小计
app.get('/cart/cart_minus',function(req,res){
    var _nums = req.query.num;
    var _price= req.query.price;
    console.log(req.query);
    if(_nums>2){
        _nums--; 
    } else {
        _nums=1;
    }
    var json={
        'total' : _nums*_price,
        'num':_nums
    }
    res.send(json);
    res.end();
});

// 计算底部商品数量总和和总金额
app.get('/cart/cart_totalmoney',function(req,res){
    var _arr = req.query.arr;
    var total_n = 0;
    var total_p = 0;
    for(var i=0;i<_arr.length;i++){
        total_n+=Number(_arr[i].n);
        total_p+=Number(_arr[i].n)*Number(_arr[i].p);
    }
    var json={
        'allnums':total_n,
        'allprice':total_p
    }
    res.send(json);
    res.end();
});
// 输入商品数量和小计
app.get('/cart/cart_enter',function(req,res){
    var _nums = req.query.num;
    var _price= req.query.price;
    console.log(req.query);
    var json={
        'total' : _nums*_price,
        'num':_nums
    }
    res.send(json);
    res.end();
});

app.listen(2333,function(){
	console.log('2333,购物车中间件已经执行')
})